import { _decorator, assetManager, loader, resources, sys } from 'cc';
import * as fgui from "fairygui-cc";
import AbstractUIWindow from '../../window/AbstractUIWindow';
import UICore from '../../../../Core/Res/UICore';
import SceneManager from '../SceneManager';
import GameScenes from '../GameScenes';
import TimerMgr from '../../../../Core/Timer/TimerMgr';
import SpineLoader from '../../../../Core/Res/SpineLoader';
import { ViewManager } from '../../base/ViewManager';
import SdkTool from '../../../../Core/Sdk/SdkTool';
import { LocalUtil } from '../../../../Core/Utils/LocalUtil';
import EventMgr from '../../../../Core/Event/EventMgr';
import UrlEnum from '../../../../Core/Res/UrlEnum';
import ConfigManager from '../../../Config/ConfigManager';
import WindowManager from '../../window/WindowManager';
import ModuleWindow from '../../window/ModuleWindow';
import WindowObject from '../../window/WindowObject';
import { ExternalGLoader } from '../../../../Core/Layer/ExternalGLoader';
import { GameModels } from '../../socket/GameModels';
import Log from '../../../../Core/Log/Log';
export class BaseStartView extends AbstractUIWindow {
    private progress: fgui.GProgressBar;
    private input: fgui.GTextInput;
    private start: fgui.GButton;
    private ipinput: fgui.GTextInput;
    private bg_full: ExternalGLoader;
    protected getResList(): Array<string> {
        return ["Start"];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Start", "BaseStartView").asCom;
    }
    protected onShow(): void {
        this.bg_full.url = `bg2.jpg`;
        EventMgr.on(`event_RespEnterLobby`, this.enterGame, this);
        EventMgr.on(`baseLogin`, this.baseLogin);
        EventMgr.on(`serverError`, this.serverError);
        this.progress.visible = false;
        this.start.visible = false;
        this.start.onClick(this.clickStart, this);
        if (sys.localStorage.getItem(`game_token_plane`)) {
            this.input.text = sys.localStorage.getItem(`game_token_plane`);
        }
        if (sys.localStorage.getItem(`game_base_ip`)) {
            this.ipinput.text = sys.localStorage.getItem(`game_base_ip`);
        } else {
            this.ipinput.text = `${UrlEnum.httpUrl}entry`;
        }
        let res = [
            "UI/Common",  //描述文件
            "UI/Common_atlas0", //纹理集
        ];
        Log.ins.log(1, `开始加载Common包`);
        resources.load(res, (err, assets) => {
            Log.ins.log(1, `Common包加载完成,显示开始游戏按钮`);
            UICore.addPackage(`Common`);
            this.start.visible = true;
        });
    }
    private serverError(): void {
        ModuleWindow.ins.openWindow(WindowObject.ins.AlertWindow, `服务器正在维护中，请稍后再试`);
    }
    private baseLogin(isSuccess: boolean): void {
        if (!isSuccess) {
            ViewManager.ins.showtips(`登录失败，请重新登录`);
            this.start.visible = true;
        }
    }
    private clickStart(): void {
        if (this.input.text == ``) {
            ViewManager.ins.showtips(`请输入账号`);
            return;
        }
        if (this.input.text === `147896`) {
            sys.localStorage.setItem(`game_token_plane`, this.input.text);
            SdkTool.ins.isFirst = true;
            SdkTool.ins.openid = `147896`;
            this.enterGame();
            return;
        }
        UrlEnum.httpUrl = this.ipinput.text;
        this.start.visible = false;
        SdkTool.ins.openid = this.input.text;
        sys.localStorage.setItem(`game_token_plane`, this.input.text);
        sys.localStorage.setItem(`game_base_ip`, this.ipinput.text);
        SdkTool.ins.sdk.login();
    }
    private enterGame(): void {
        this.start.visible = false;
        this.progress.visible = true;
        this.progress.max = 100;
        this.progress.value = 0;
        let res = [
            "UI/Main",  //描述文件
            "UI/Main_atlas0", //纹理集
            "UI/Plane",  //描述文件
            "UI/Plane_atlas0", //纹理集
            "UI/Font",  //描述文件
            "UI/Font_atlas0", //纹理集
        ];
        Log.ins.log(1, `开始加载游戏主体资源`);
        resources.load(res, (finsh, toal, item) => {
            this.progress.max = toal;
            this.progress.value = finsh;
        }, (err, assets) => {
            Log.ins.log(1, `游戏主体资源加载完毕`);
            UICore.addPackage(`Main`);
            UICore.addPackage(`Plane`);
            UICore.addPackage(`Font`);
            SceneManager.ins.enterScene(GameScenes.main);
        });
    }
}
